package com.lottery.material.service.admin.conf.impl;

import com.lottery.material.common.Constants;
import com.lottery.material.dao.conf.AppChannelDictMapper;
import com.lottery.material.enumeration.common.CommonEnum;
import com.lottery.material.enumeration.conf.AppChannelDictEnum;
import com.lottery.material.pojo.conf.AppChannelDict;
import com.lottery.material.pojo.page.Page;
import com.lottery.material.pojocustom.conf.AppChannelDictCustom;
import com.lottery.material.service.admin.conf.AppChannelDictService;
import com.lottery.material.util.AssertUtil;
import com.lottery.material.util.CalendarUtil;
import com.lottery.material.util.EnumUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Description：APP渠道字典服务接口实现类
 * Created by 朱正磊 on 2019-05-14 17:34
 */
@Service("adminAppChannelDictService")
public class AppChannelDictServiceImpl implements AppChannelDictService {

    private final AppChannelDictMapper appChannelDictMapper;

    @Autowired
    public AppChannelDictServiceImpl(AppChannelDictMapper appChannelDictMapper) {
        this.appChannelDictMapper = appChannelDictMapper;
    }

    /**
     * description：新增一条数据
     * user 朱正磊
     * time 2019-04-20 15:14
     * @param appChannelDict 对象
     * @return 返回持久化操作数据库状态值
     */
    @Override
    public Integer add(AppChannelDict appChannelDict) {
        String channelCode = this.genChannelCode();
        appChannelDict.setChannelCode(channelCode);//设置渠道编号
        appChannelDict.setCreateTime(new Date());//初始化创建时间
        appChannelDict.setState((Integer) CommonEnum.STATE_OFF.getValue());//默认下架
        return appChannelDictMapper.insertSelective(appChannelDict);
    }

    /**
     * description：根据主键物理删除
     * user 朱正磊
     * time 2019-04-20 15:13
     * @param id 主键
     * @return 返回持久化操作数据库状态值
     */
    @Override
    public Integer deleteById(Integer id) {
        return appChannelDictMapper.deleteByPrimaryKey(id);
    }

    /**
     * description：根据主键查询唯一数据
     * user 朱正磊
     * time 2019-04-20 15:15
     * @param id 主键
     * @return 返回唯一数据
     */
    @Override
    public AppChannelDict findById(Integer id) {
        return appChannelDictMapper.selectByPrimaryKey(id);
    }

    /**
     * description：根据主键修改一条数据
     * user 朱正磊
     * time 2019-04-20 15:16
     * @param appChannelDict 对象
     * @return 返回持久化操作数据库状态值
     */
    @Override
    public Integer updateById(AppChannelDict appChannelDict) {
        return appChannelDictMapper.updateByPrimaryKeySelective(appChannelDict);
    }

    /**
     * description：查询扩展类分页列表
     * user 朱正磊
     * time 2019-04-17 17:01
     * @param page 分页对象
     * @return 返回扩展类分页列表数据
     */
    @Override
    public List<AppChannelDictCustom> findCustomListPage(Page page) {
        List<AppChannelDictCustom> list = appChannelDictMapper.findCustomListPage(page);
        list.forEach(this::setCustom);
        return list;
    }

    /**
     * description：根据主键查询唯一扩展类数据
     * user 朱正磊
     * time 2019-04-17 17:03
     * @param id 主键
     * @return 返回唯一扩展类数据
     */
    @Override
    public AppChannelDictCustom findCustomById(Integer id) {
        AppChannelDictCustom custom = appChannelDictMapper.findCustomById(id);
        this.setCustom(custom);
        return custom;
    }

    /**
     * description：设置扩展类的扩展属性
     * user 朱正磊
     * time 2019-04-17 17:48
     * @param custom 扩展类对象
     */
    private void setCustom(AppChannelDictCustom custom) {
        //创建时间
        custom.setCreateTimeFormat(CalendarUtil.dateToString(custom.getCreateTime()));
        //渠道平台（1：苹果；2：安卓；3：官网）
        custom.setChannelPlatformDesc(EnumUtil.getDescriptionByCodeValue(AppChannelDictEnum
                .CHANNEL_PLATFORM, custom.getChannelPlatform(), AppChannelDictEnum.class));
        //状态 1：上架；2：下架
        custom.setStateDesc(EnumUtil.getDescriptionByCodeValue(CommonEnum.STATE, custom.getState(), CommonEnum.class));
    }

    /**
     * description：生成渠道编号
     * user 朱正磊
     * time 2019-04-19 17:33
     * @return 返回渠道编号，比如第一个：CHC00001
     */
    private String genChannelCode() {
        //获取最大的渠道编号
        String maxChannelCode = appChannelDictMapper.findMaxChannelCode();
        String serialNo;
        String channelCode;
        if (AssertUtil.isEmpty(maxChannelCode)) {
            serialNo = "00001";
        } else {
            String maxSerialNo = maxChannelCode.substring(3);
            serialNo = String.format("%05d", Integer.valueOf(maxSerialNo) + 1);
        }
        channelCode = Constants.CHANNEL_CODE_PREFIX + serialNo;
        return channelCode;
    }

}
